'''
炸金花棋牌游戏
需求：
1. 允许⽤用户⼀一次性输⼊入多个玩家姓名，不不限个数，然后为每个玩家随机⽣生成3张牌
2. 你只有⼀一付扑克牌，确保发出去的每张牌不不重样
3. 牌需要有⿊黑桃、红桃、⽅方⽚片、梅梅花之分
'''
import random

HUASE_LIST = ['方块', '梅花', '红桃', '黑桃']  # 花色，常量
PAIHAO_LIST = [str(i) for i in range(2, 11)] + ['J', 'Q', 'K', 'A']  # 数字，常量
score_map = {}


# 生成牌
def get_pai(random_pai_list):
    while True:
        pai = random.choice(HUASE_LIST) + random.choice(PAIHAO_LIST)  # 随机生成
        if pai in random_pai_list:
            # 牌存在，则继续循环
            continue
        else:
            return pai  # 返回对应的牌


# 对牌型进行计分
def jifen(pai_list):
    '''
    牌点从大到小为：A、K、Q、J、10、9、8、7、6、5、4、3、2，各花色不分大小
    '''
    # 将花色和数字分开
    player_huase_list = []
    player_number_list = []
    # 为了方便计算，需要对牌面的数字进行转，J=11，Q=12，K=13,A=14
    for pai in pai_list:
        player_huase_list.append(pai[0:2])

        if pai[2:] == 'J':
            player_number_list.append(11)
        elif pai[2:] == 'Q':
            player_number_list.append(12)
        elif pai[2:] == 'K':
            player_number_list.append(13)
        elif pai[2:] == 'A':
            player_number_list.append(14)
        else:
            player_number_list.append(int(pai[2:]))

    player_number_list.sort()  # 最后将牌的数字进行排序

    # 特殊：花色不同的235
    if player_number_list == [2, 3, 5]:  # 存在特殊花色
        pai_type = ['特殊', '普通']
        pai_score = [100, 10]
        return pai_type, pai_score

    # 豹子（炸弹）：三张点相同的牌。例：AAA、222。
    if player_number_list[0] == player_number_list[1] == player_number_list[2]:
        pai_type = '豹子'
        pai_score = player_number_list[0] * 3 * 90
        return pai_type, pai_score

    # 对子：两张点数相同的牌。例：223，334。
    elif (player_number_list[0] == player_number_list[1]) or (player_number_list[1] == player_number_list[2]):
        pai_type = '对子'
        pai_score = (player_number_list[0] + player_number_list[1] + player_number_list[2]) * 50
        return pai_type, pai_score

    elif (player_number_list[1] - player_number_list[0] == 1) and (player_number_list[2] - player_number_list[1] == 1):
        # 顺金（同花顺、色托）：花色相同的顺子。例：黑桃456、红桃789。最大的顺金为花色相同的QKA，最小的顺金为花色相同的123。
        if player_number_list[0] == player_number_list[1] == player_number_list[2]:
            pai_type = '顺金'
            pai_score = (player_number_list[0] + player_number_list[1] + player_number_list[2]) * 80
            return pai_type, pai_score
        # 顺子（拖拉机）：花色不同的顺子。例：黑桃5红桃6方块7。最大的顺子为花色不同的QKA，最小的顺子为花色不同的123。
        else:
            pai_type = '顺子'
            pai_score = (player_number_list[0] + player_number_list[1] + player_number_list[2]) * 60
            return pai_type, pai_score

    # 金花（色皮）：花色相同，非顺子。例：黑桃368，方块145。
    elif player_number_list[0] == player_number_list[1] == player_number_list[2]:
        pai_type = '金花'
        pai_score = (player_number_list[0] + player_number_list[1] + player_number_list[2]) * 70
        return pai_type, pai_score

    else:
        pai_type = '普通'
        pai_score = player_number_list[0] + player_number_list[1] + player_number_list[2]
        return pai_type, pai_score


# 对玩家进行发牌
def fapai(player_list):
    player_pai_dict = {}
    random_pai_list = []  # 随机生成牌面,用于存放随机生成的牌.
    for i in range(len(player_list) * 3):  # 每个玩家有3张牌，总发牌数为玩家数*3
        random_pai_list.append(get_pai(random_pai_list))

    for player in player_list:
        player_pai_dict[player] = []
        for i in range(3):
            pai = random.choice(random_pai_list)  # 随机从牌面中获取一个
            player_pai_dict[player].append(pai)  # 添加用户牌面
            random_pai_list.remove(pai)  # 删除牌面

    return player_pai_dict


# 主程序
def main():
    player_names = input('您好，欢迎来到炸金花游戏!请输入玩家名字，以,作为分隔符（英文字符的逗号）:')
    player_list = player_names.split(',')
    player_pai_dict = fapai(player_list)
    player_pai_type_dict = {}
    winner_player = ''  # 赢家
    winner_score = 0  # 分数
    for k, v in player_pai_dict.items():
        pai_type, pay_score = jifen(v)
        # 如果是列表的话，就说明有235的牌，需要进行特殊处理
        if isinstance(pai_type, list):
            for i in range(len(pai_type)):
                if pai_type[i] in player_pai_type_dict:
                    player_pai_type_dict[pai_type[i]][k] = pay_score[i]
                else:
                    player_pai_type_dict[pai_type][i] = {k: pay_score[i]}
        else:
            if pai_type in player_pai_type_dict:
                player_pai_type_dict[pai_type][k] = pay_score
            else:
                player_pai_type_dict[pai_type] = {k: pay_score}  # {'普通':{'张三': 23}}

    # 豹子 > 顺金 > 金花 > 顺子 > 对子 > 单张
    # 特殊牌型在普通时比较大小按单张牌型来算。当豹子存在时，特殊牌型 > 豹子。
    # 判断有没有豹子
    if '豹子' in player_pai_type_dict:
        # 判断有没有特殊
        if '特殊' in player_pai_type_dict:
            if len(player_pai_type_dict['特殊'].keys()) == 1:
                winner_player = list(player_pai_type_dict['特殊'].keys())[0]
            else:
                for player, score in player_pai_type_dict['特殊']:
                    if int(score) > winner_score:
                        winner_player = player  # 赋值
                        winner_score = score  # 赋值
        # 没有特殊，则就是普通的，直接豹子比大小
        else:
            if len(player_pai_type_dict['豹子'].keys()) == 1:
                winner_player = list(player_pai_type_dict['豹子'].keys())[0]
            else:
                for player, score in player_pai_type_dict['豹子']:
                    if int(score) > winner_score:
                        winner_player = player  # 赋值
                        winner_score = score  # 赋值
    # 判断有没有顺金
    elif '顺金' in player_pai_type_dict:
        if len(player_pai_type_dict['顺金'].keys()) == 1:
            winner_player = list(player_pai_type_dict['顺金'].keys())[0]
        else:
            for player, score in player_pai_type_dict['顺金']:
                if int(score) > winner_score:
                    winner_player = player  # 赋值
                    winner_score = score  # 赋值
    # 判断有没有金花
    elif '金花' in player_pai_type_dict:
        if len(player_pai_type_dict['金花'].keys()) == 1:
            winner_player = list(player_pai_type_dict['金花'].keys())[0]
        else:
            for player, score in player_pai_type_dict['金花']:
                if int(score) > winner_score:
                    winner_player = player  # 赋值
                    winner_score = score  # 赋值
    # 判断有没有顺子
    elif '顺子' in player_pai_type_dict:
        if len(player_pai_type_dict['顺子'].keys()) == 1:
            winner_player = list(player_pai_type_dict['顺子'].keys())[0]
        else:
            for player, score in player_pai_type_dict['顺子']:
                if int(score) > winner_score:
                    winner_player = player  # 赋值
                    winner_score = score  # 赋值
    # 判断有没有对子
    elif '对子' in player_pai_type_dict:
        if len(player_pai_type_dict['对子'].keys()) == 1:
            winner_player = list(player_pai_type_dict['对子'].keys())[0]
        else:
            for player, score in player_pai_type_dict['对子']:
                if int(score) > winner_score:
                    winner_player = player  # 赋值
                    winner_score = score  # 赋值
    # 剩下的就是普通比大小了
    else:
        for player, score in player_pai_type_dict['普通']:
            if int(score) > winner_score:
                winner_player = player  # 赋值
                winner_score = score  # 赋值

    print(player_pai_dict)
    print(player_pai_type_dict)
    print(f'恭喜{winner_player}赢得本次比赛!')


if __name__ == "__main__":
    main()
